/**  
* @Title: ICommonDAOImpl.java
* @Package com.bj58.zhaopin.activity.dao
* @Description: TODO
* @author yangning02@58.com
* @date 2014-1-10 下午1:43:07
* @Copyright: 2014 www.58.com Inc. All rights reserved.
* @version V1.0  
*/

package com.bj58.migrate.helper.dao;

import java.util.List;
import java.util.Map;

import com.bj58.sfft.utility.dao.basedao.DAOHelper;
import com.bj58.sfft.utility.dao.basedao.IPreparedStatementHandler;

/**
 * @ClassName ICommonDAOImpl
 * @Description TODO
 * @author yangning02@58.com
 * @date 2014-1-10
 */
public class CommonDAOImpl implements ICommonDAO{

	private static DAOHelper daoHelper = DatabaseHelper.getInstance().getDaoHelper();
	
	public int getCount(Class clazz, String where) throws Exception {

		return daoHelper.sql.getCount( clazz, where );
	}

	public int getCountBySql( String sql, Object... param ) throws Exception{

		return daoHelper.sql.getCountBySQL( sql, param );
	}

	public int getCountByParamMap( Class clazz, Map< String, Object > paramMap ) throws Exception{

		return daoHelper.sql.getCount( clazz, paramMap );
	}
	/**
	 * @Method: getByid 
	 * @Description: TODO
	 * @param id
	 * @return
	 * @throws Exception 
	 * @see com.bj58.migrate.helper.dao.activity.dao.ICommonDAO#getByid(int) 
	 */
	@Override
	public Object getByid(Class clzz,int id) throws Exception {

		return daoHelper.sql.get( clzz, id );
	}
	
	
	
	/**
	 * @Method: getByid 
	 * @Description: TODO
	 * @param id
	 * @return
	 * @throws Exception 
	 * @see com.bj58.migrate.helper.dao.activity.dao.ICommonDAO#getByid(long) 
	 */
	@Override
	public Object getByid(Class clzz,long id) throws Exception {

		return daoHelper.sql.get( clzz, id );
	}

	/**
	 * @Method: getListBySql 
	 * @Description: TODO
	 * @param classzz
	 * @param sql
	 * @param param
	 * @return
	 * @throws Exception 
	 * @see com.bj58.migrate.helper.dao.activity.dao.ICommonDAO#getListBySql(java.lang.Class, java.lang.String, java.lang.Object) 
	 */
	public <T> List<T>  getListBySql(Class<T> clazz, String sql, Object... param)throws Exception {

		return daoHelper.sql.getListBySQL( clazz, sql, param );
	}

	/**
	 * @Method: insert 
	 * @Description: TODO
	 * @param obj
	 * @return
	 * @throws Exception 
	 * @see com.bj58.migrate.helper.dao.activity.dao.ICommonDAO#insert(java.lang.Object) 
	 */
	public Long insert( Object obj ) throws Exception{

		return ( Long )daoHelper.sql.insert( obj );
	}

	/**
	 * @Method: update 
	 * @Description: TODO
	 * @param obj
	 * @return
	 * @throws Exception 
	 * @see com.bj58.migrate.helper.dao.activity.dao.ICommonDAO#update(java.lang.Object) 
	 */
	public void update(Object obj) throws Exception {

		daoHelper.sql.upateEntity( obj );
	}

	/**
	 * @Method: updateOneField 
	 * @Description: TODO
	 * @param id
	 * @param key
	 * @param value
	 * @return
	 * @throws Exception 
	 * @see com.bj58.migrate.helper.dao.activity.dao.ICommonDAO#updateOneField(java.lang.Object, java.lang.String, java.lang.String) 
	 */
	@Override
	public void updateOneField(Class classzz,Object id, String key, String value)throws Exception {
		StringBuilder sb = new StringBuilder();
		sb.append(key).append(" = ").append(value);
		StringBuilder sbWhere = new StringBuilder();
		sbWhere.append(" id = ").append(id);
		daoHelper.sql.updateByCustom( classzz, sb.toString(), sbWhere.toString() );;
	}

	/**
	 * @Method: execBySQL
	 * @param sql
	 */
	@Override
	public int execBySQL( String sql ) throws Exception{

		int result = daoHelper.sql.execBySQL( sql );
		return result;
	}

	/**
	 * @Method: pageListBySql 
	 * @Description: TODO
	 * @param clazz
	 * @param sql
	 * @param param
	 * @return
	 * @throws Exception 
	 * @see com.bj58.migrate.helper.dao.activity.dao.ICommonDAO#pageListBySql(java.lang.Class, java.lang.String, java.lang.Object[]) 
	 */
	@SuppressWarnings( "unchecked" )
	@Override
	public <T> List<T> pageListBySql(Class<T> clazz, String condition,int page,int pageSize,String orderBy) throws Exception {

		return daoHelper.sql.getListByPage( clazz, condition, "*", page, pageSize, orderBy, 2 );
	}

	/**
	 * 根据参数对象获取分页信息
	 * 
	 * @param clazz
	 * @param params
	 * @param page
	 * @param pageSize
	 * @param orderBy
	 * @return
	 * @throws Exception
	 */
	public < T >List< T > pageListBySql( Class< T > clazz, Map< String, Object > params, String field, int page, int pageSize, String orderBy ) throws Exception{

		if( field != null && field.length() < 0 ){
			field = "*";
		}
		return daoHelper.sql.getListByPage( clazz, params, field, page, pageSize, orderBy );
	}

	public Object execInsert( String sql, IPreparedStatementHandler handler ) throws Exception{

		System.out.println( "daoHelper:" + daoHelper );
		return daoHelper.execInsert( sql, handler );
	}
}

